// FacebookApplicationSettings.cs
// Facebook/FrameworkWeb/Web
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Configuration;
using System.Diagnostics;
using System.Web.Configuration;

namespace Facebook.Web.Configuration {

    /// <summary>
    /// Represents configuration information for a single Facebook application.
    /// </summary>
    public sealed class FacebookApplicationSettings : ConfigurationElement {

        private static readonly ConfigurationProperty NameProperty =
            new ConfigurationProperty("name", typeof(string), null,
                                      new WhiteSpaceTrimStringConverter(),
                                      new StringValidator(1),
                                      ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);

        private static readonly ConfigurationProperty ApiKeyProperty =
            new ConfigurationProperty("apiKey", typeof(string), null,
                                      new WhiteSpaceTrimStringConverter(),
                                      new StringValidator(1),
                                      ConfigurationPropertyOptions.IsRequired);

        private static readonly ConfigurationProperty SecretProperty =
            new ConfigurationProperty("secret", typeof(string), null,
                                      new WhiteSpaceTrimStringConverter(),
                                      new StringValidator(1),
                                      ConfigurationPropertyOptions.IsRequired);

        private static readonly ConfigurationProperty TypeProperty =
            new ConfigurationProperty("type", typeof(FacebookApplicationType), FacebookApplicationType.UserApplication);

        private static ConfigurationPropertyCollection AllProperties = BuildProperties();

        /// <summary>
        /// The application key used to uniquely identify the application.
        /// </summary>
        [ConfigurationProperty("apiKey", IsRequired = true, DefaultValue = "")]
        [StringValidator(MinLength = 1)]
        public string ApiKey {
            get {
                return (string)base[ApiKeyProperty];
            }
            set {
                base[ApiKeyProperty] = value;
            }
        }

        /// <summary>
        /// The name of the application used to uniquely identify it on the server.
        /// </summary>
        [ConfigurationProperty("name", IsRequired = true, IsKey = true, DefaultValue = "")]
        [StringValidator(MinLength = 1)]
        public string Name {
            get {
                return (string)base[NameProperty];
            }
            set {
                base[NameProperty] = value;
            }
        }

        /// <internalonly />
        protected override ConfigurationPropertyCollection Properties {
            get {
                return AllProperties;
            }
        }

        /// <summary>
        /// The application secret used to authenticate API calls issued by the application.
        /// </summary>
        [ConfigurationProperty("secret", IsRequired = true, DefaultValue = "")]
        [StringValidator(MinLength = 1)]
        public string Secret {
            get {
                return (string)base[SecretProperty];
            }
            set {
                base[SecretProperty] = value;
            }
        }

        /// <summary>
        /// The application type used to indicate whether the application can be added to a user's
        /// profile, a presence page, or both.
        /// </summary>
        [ConfigurationProperty("type", DefaultValue = FacebookApplicationType.UserApplication)]
        public FacebookApplicationType Type {
            get {
                return (FacebookApplicationType)base[TypeProperty];
            }
            set {
                base[TypeProperty] = value;
            }
        }

        private static ConfigurationPropertyCollection BuildProperties() {
            ConfigurationPropertyCollection props = new ConfigurationPropertyCollection();
            props.Add(NameProperty);
            props.Add(ApiKeyProperty);
            props.Add(SecretProperty);
            props.Add(TypeProperty);

            return props;
        }
    }
}
